package com.tan.lbs.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tan.lbs.entity.Designer;
import com.tan.lbs.service.DesignerService;
import com.tan.lbs.service.impl.DesignerServiceImpl;
import com.tan.lbs.util.UniqueID;

/**
 * Servlet implementation class DesignerServlet
 */
@WebServlet("/DesignerService")
public class DesignerServlet extends HttpServlet {
	
	private DesignerService designerService;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DesignerServlet() {
        super();

    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        designerService = new DesignerServiceImpl();
        
		String method = request.getParameter("method");

		
		if("isExistedUser".equalsIgnoreCase(method)) {	
			String username = request.getParameter("username");
			  if(designerService.isExistedUser(username)) {
		        	response.getWriter().print("1");  
		        } else {
		        	// 0 stands for the username is not used.
		        	response.getWriter().print("0");  
		       }

		} else if("register".equalsIgnoreCase(method)) {
			String username = request.getParameter("username");
			String password = (String)request.getParameter("password");
			String email = (String)request.getParameter("email");
			String confirmpassword = (String)request.getParameter("confirmpassword");
			String org = (String)request.getParameter("org");
			
			Designer designer = new Designer();
			designer.setEmail(email);
			designer.setMaxGameNumber(10);
			designer.setOrganizations(org);
			designer.setUsername(username);
			designer.setPasswd(password);
			
			List<String> errorMsgs = designerService.register(designer, confirmpassword);
			if(errorMsgs.size() == 0) {
		        HttpSession s = request.getSession();
	            s.setAttribute("username", username);
	            s.setAttribute("designerID", designer.getDesignerID());
	            request.getRequestDispatcher("/designerDashBoard.jsp").forward(request, response);
	        } else {
	        	request.setAttribute("errorMsgs", errorMsgs);
	        	request.getRequestDispatcher("/register_designer.jsp").forward(request, response);
	        }
		
       } else if("login".equalsIgnoreCase(method)) {
	   		String username = request.getParameter("username");
	   		String password = request.getParameter("password");
	   		String remember = request.getParameter("remember");
    	   HttpSession s = request.getSession();
           s.setAttribute("username", "");
           s.setAttribute("designerID", "");
           String designerID = designerService.login(username, password);
           if(designerID !=null) {
	        	if("on".equals(remember)) {
	        		 Cookie cookie = new Cookie("username",username);
	        		 Cookie cookie2 = new Cookie("designerID",designerID);
	        	     cookie.setMaxAge(24*3600);
	        	     cookie2.setMaxAge(24*3600);
	        	     response.addCookie(cookie);
	        	     response.addCookie(cookie2);
	        	}
	            s.setAttribute("username", username);
	            s.setAttribute("designerID", designerID);
	            request.getRequestDispatcher("/designerDashBoard.jsp").forward(request, response);
	        } else {
	    	   request.setAttribute("message", "Invalid account or password");
	    	   request.getRequestDispatcher("/login_designer.jsp").forward(request, response);
	       }
    	   
       } else if("logout".equalsIgnoreCase(method)) {
    		HttpSession s = request.getSession(false);
    		if(s!=null) {
    			s.removeAttribute("username");
    		}
    		Cookie[]  cookies = request.getCookies();
    		if(cookies!=null) {
    		for(int i = 0 ; i < cookies.length; ++i) {
    			if(cookies[i].getName().equals("username") || cookies[i].getName().equals("designerID")) {
    				cookies[i].setMaxAge(0);
    				response.addCookie(cookies[i]);
    			}
    		  }
    		}
    		response.sendRedirect("logout_designer.html");
       }

}
	}


